package com.code2roc.jarmanage.util;

import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

public class ConvertUtil {
    public static String convert2String(Object obj) {
        String result;
        if (!isNull(obj)) {
            try {
                result = String.valueOf(obj);
            } catch (Exception e) {
                LogUtil.error(e);
                result = StringUtil.EMPTY;
            }
        } else {
            result = StringUtil.EMPTY;
        }
        return result;
    }

    public static Integer convert2Int(Object obj) {
        Integer result;
        if (!isNull(obj)) {
            if (obj.getClass() == BigDecimal.class) {
                BigDecimal bigDecimal = (BigDecimal) obj;
                return bigDecimal.intValue();
            } else {
                try {
                    result = Integer.parseInt(String.valueOf(obj));
                } catch (Exception e) {
                    LogUtil.error(e);
                    result = 0;
                }
            }

        } else {
            result = 0;
        }
        return result;
    }

    public static Double convert2Double(Double obj, int scale) {
        Double result;
        if (!isNull(obj)) {
            try {
                BigDecimal b = new BigDecimal(obj);
                result = b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            } catch (Exception e) {
                e.printStackTrace();
                ;
                result = 0.0;
            }
        } else {
            result = 0.0;
        }
        return result;
    }

    public static Double convert2Double(Object obj) {
        Double result;
        if (!isNull(obj)) {
            try {
                result = Double.parseDouble(String.valueOf(obj));
            } catch (Exception e) {
                e.printStackTrace();
                ;
                result = 0.0;
            }
        } else {
            result = 0.0;
        }
        return result;
    }

    public static Boolean convert2Boolean(Object obj) {
        Boolean result;
        if (!isNull(obj)) {
            try {
                result = Boolean.parseBoolean(String.valueOf(obj));
            } catch (Exception e) {
                e.printStackTrace();
                ;
                result = false;
            }
        } else {
            result = false;
        }
        return result;
    }

    public static boolean isNull(Object obj) {
        if (obj == null) {
            return true;
        } else {
            return false;
        }
    }

    public static <T1, T2> T2 convertSameStructObj(T1 fromBean, T2 toBean) {
        try {
            List<Field> fromFieldList = Arrays.asList(fromBean.getClass().getDeclaredFields());
            List<Field> toFieldList = Arrays.asList(toBean.getClass().getDeclaredFields());
            for (Field fromfield : fromFieldList) {
                for (Field tofield : toFieldList) {
                    if (tofield.getName().toLowerCase().equals(fromfield.getName().toLowerCase())) {
                        fromfield.setAccessible(true);
                        Object value = fromfield.get(fromBean);
                        tofield.setAccessible(true);
                        tofield.set(toBean, value);
                    }
                }
            }
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return toBean;
    }
}
